package com.feeyo.prophet.actions.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.feeyo.llm.agent.engine.Action;
import com.feeyo.llm.agent.engine.ActionResult;
import com.feeyo.llm.agent.engine.ExecuteOptions;
import com.feeyo.llm.agent.engine.ParameterScheme;
import com.feeyo.prophet.pojo.action.functioncall.FunctionCall;
import com.feeyo.prophet.pojo.action.functioncall.common.ConversationProperties;
import com.feeyo.prophet.pojo.action.functioncall.flight.common.BasicTypeDefaultList;
import com.feeyo.prophet.config.ParametersSingletonManager;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class ConversationNonBlockAction implements Action {

	private boolean useChinesePrompt = false;

	public ConversationNonBlockAction() {
		this(false);
	}

	public ConversationNonBlockAction(boolean useChinesePrompt) {
		this.useChinesePrompt = useChinesePrompt;
	}
	//
	@Override
	public String human() {
		return "会话工具";
	}
	//
	@Override
	public String name() {
		return "conversationTool";
	}

	@Override
	public String eg() {
		return "";
	}

	@Override
	public String description() {
		if (useChinesePrompt) {
			return "当信息不足且对方需要提供更多细节时，请使用此会话工具用于追问";
		} else  {
			return "When there is insufficient information and the other party needs to provide additional details, please use this conversational tool for follow-up questions";
		}
	}

	@Override
	public String parameters() {
		return ParametersSingletonManager.getParameters("ConversationNonBlockAction", () -> {
			FunctionCall functionCall = new FunctionCall();
			functionCall.setType("object");
			functionCall.setRequired(Lists.newArrayList("originalText"));
			ConversationProperties properties = new ConversationProperties();
			properties.setQuestionText(new BasicTypeDefaultList("string", "问题文本"));
			functionCall.setProperties(properties);
			return functionCall.toString();
		});
	}

	@Override
	public ParameterScheme defaultParameters() {
		Map<String, ParameterScheme.Property> properties = new HashMap<>();
		// QuestionText property
		ParameterScheme.Property questionTextProperty = new ParameterScheme.Property();
		questionTextProperty.setType("string");
		questionTextProperty.setDescription("问题文本");
		properties.put("originalText", questionTextProperty);
		// Create ParameterScheme object
		ParameterScheme parameterScheme = new ParameterScheme("object", List.of("originalText"), properties);
		return parameterScheme;
	}

	@Override
	public ActionResult run(String question, String input, ExecuteOptions options) throws Exception {
		JSONObject jsonObject = JSON.parseObject(input);
		String questionText = (String) jsonObject.get("questionText");
		ActionResult result = new ActionResult(HttpStatus.OK.value(), questionText);
		result.setFollowUpQuestion(true);
		return result;
	}
}